ПРИМЕР № 2
«Работа с
табличными данными»

Исходный код примера:
package tsn.javase.demo02;
public class Form1 extends javax.swing.JFrame {
public Form1() {
initComponents();
}
@SuppressWarnings("unchecked")
// //GEN-BEGIN:initComponents
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Работа с табличными данными");
setResizable(false);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowActivated(java.awt.event.WindowEvent evt) {
this_windowActivated(evt);
}
});
getContentPane().setLayout(null);
jScrollPane1.setBorder(null);
jScrollPane1.setToolTipText("");
jTable1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Поле 1", "Поле 2", "Поле 3", "Поле 4"
}
) {
Class[] types = new Class [] {
java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class
};
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
});
jTable1.setToolTipText("");
jTable1.setCellSelectionEnabled(true);
jTable1.setFillsViewportHeight(true);
jTable1.setRowMargin(2);
jScrollPane1.setViewportView(jTable1);
jTable1.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);
getContentPane().add(jScrollPane1);
jScrollPane1.setBounds(18, 11, 198, 100);
jButton1.setText("Случайные числа");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
getContentPane().add(jButton1);
jButton1.setBounds(234, 11, 121, 23);
jButton2.setText("Сумма цифр");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
getContentPane().add(jButton2);
jButton2.setBounds(234, 52, 120, 23);
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText("jLabel1");
getContentPane().add(jLabel1);
jLabel1.setBounds(234, 81, 110, 14);
setSize(new java.awt.Dimension(376, 155));
setLocationRelativeTo(null);
}// //GEN-END:initComponents
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
//GEN-FIRST:event_jButton1ActionPerformed
// Случайные числа
long k;
for (int i = 0; i < jTable1.getRowCount(); i++) {
for (int j = 0; j < jTable1.getColumnCount(); j++) {
k = Math.round(Math.random() * 10);
jTable1.setValueAt(String.valueOf(k), i, j);
}
}
jButton2ActionPerformed(null);
}//GEN-LAST:event_jButton1ActionPerformed
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
//GEN-FIRST:event_jButton2ActionPerformed
// Сумма цифр
long s = 0;
try {
for (int i = 0; i < jTable1.getRowCount(); i++) {
for (int j = 0; j < jTable1.getColumnCount(); j++) {
s += Integer.parseInt(jTable1.getValueAt(i, j).toString());
}
}
jLabel1.setText(String.valueOf(s));
} catch (Exception ee) {
jLabel1.setText("Error");
}
}//GEN-LAST:event_jButton2ActionPerformed
private void this_windowActivated(java.awt.event.WindowEvent evt) {
//GEN-FIRST:event_this_windowActivated
// Инициализация таблицы
jScrollPane1.setColumnHeaderView(null); // Убрать заголовки колонок
jScrollPane1.setRowHeaderView(null); // Убрать заголовки строк
for (int i = 0; i < jTable1.getColumnCount(); i++) {
// Установка ширины столбцов
jTable1.getColumnModel().getColumn(i).setMaxWidth(60);
}
for (int i = 0; i < jTable1.getRowCount(); i++) {
for (int j = 0; j < jTable1.getColumnCount(); j++) {
// Инициализация ячеек "0"
jTable1.setValueAt("0", i, j);
}
}
jButton1ActionPerformed(null); // Случайные числа
}//GEN-LAST:event_this_windowActivated
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Windows".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Form1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Form1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Form1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Form1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Form1().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
// End of variables declaration//GEN-END:variables
}